package com.keymozy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keymozy.clients.ImgClient;
import com.keymozy.mapper.DeptMapper;
import com.keymozy.mapper.StaffMapper;
import com.keymozy.pojo.Dept;
import com.keymozy.pojo.Position;
import com.keymozy.pojo.Staff;
import com.keymozy.pojo.vo.VoStaff;
import com.keymozy.service.DeptService;
import com.keymozy.service.PositionService;
import com.keymozy.service.StaffService;
import com.keymozy.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IStaffService extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private ImgClient imgClient;
    @Value("${image.path}")
    private String imgPath;

    /**
     * 获取所有员工列表
     * @param currentPage 当前页
     * @param pageSize 页大小
     * @param s 查询信息
     * @return
     */
    @Override
    public Map getAll(int currentPage, int pageSize, Staff s) {
        System.out.println(s+"===================");
        log.info("进入了staffService.getAll");
        Staff staff = (Staff) TokenUtil.data.get();
        Page<Staff> page = new Page(currentPage, pageSize);
        IPage<Staff> pages;
        LambdaQueryWrapper<Staff> lqw = new LambdaQueryWrapper<>();
        System.out.println("staff"+s);
        if(s!=null){
            lqw.like(s.getId()!=null,Staff::getId,s.getId());
            lqw.like(s.getUname() != null && !"".equals(s.getUname()), Staff::getUname, s.getUname());
            lqw.like(s.getPhone() != null && !"".equals(s.getPhone()), Staff::getPhone, s.getPhone());
            lqw.like(s.getTime() != null && !"".equals(s.getTime()), Staff::getTime, s.getTime());
            lqw.like(s.getDid() != null, Staff::getDid, s.getDid());
            lqw.like(s.getPid() != null, Staff::getPid, s.getPid());
        }
//        lqw.eq(Staff::getPid,2);

//        Dept dept = deptService.getOne(new LambdaQueryWrapper<Dept>().eq(Dept::getSid, staff.getId()));
//        log.info("dept:"+dept);
//        if (dept!=null){
//            lqw.eq(Staff::getDid,dept.getId());
//        }
//        lqw.ne(Staff::getId,staff.getId());
//        pages = page(page, lqw);
        //获得对应部门
//        getDept(pages.getRecords());
//        //获得对应职位
//        getPosition(pages.getRecords());
        pages = this.page(page, lqw);
        List<Staff> records = pages.getRecords();
        for (Staff record : records) {
            Integer pid = record.getPid();
            Integer did = record.getDid();
            record.setPosition(positionService.getOne(new LambdaQueryWrapper<Position>()
                    .eq(Position::getId,pid)));
            record.setDept(deptService.getOne(new LambdaQueryWrapper<Dept>().eq(Dept::getId,did)));
        }
        Map data = new HashMap();
        data.put("total",pages.getTotal());
        data.put("data",pages.getRecords());
        log.info("pages:" + data);
        return data;
    }

    /**
     * 添加
     * @param staff
     * @return
     */
    @Override
    public boolean add(Staff staff) {
        LambdaQueryWrapper<Staff> lqw = new LambdaQueryWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        lqw.eq(Staff::getPhone,staff.getPhone());
        Staff s = getOne(lqw);
        staff.setTime(format);
        if (s != null){
            return false;
        }
        return save(staff);
    }

    /**
     * 修改
     * @param staff
     * @return
     */
    @Override
    public boolean alter(Staff staff) {
        log.info("进入了staff修改");
        log.info("staff:"+staff);
        LambdaQueryWrapper<Staff> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Staff::getId,staff.getId());
        return update(staff,lqw);
    }

    /**
     * 删除
     * @param staffs
     * @return
     */
    @Transactional
    @Override
    public boolean delete(Staff[] staffs) {
        for (Staff staff : staffs) {
            if (!removeById(staff.getId())){
                return false;
            }
            imgClient.rmImg(this.imgPath+"/"+staff.getPhone());
        }
        return true;
    }

    @Override
    public List<VoStaff> registerEchart(String beginTime, String endTime) {
        List<Staff> staff = baseMapper.echartDateCountStaff(beginTime, endTime);
        ArrayList<VoStaff> voStaffs = new ArrayList<>();
        for (Staff staff1 : staff) {
            VoStaff voStaff = new VoStaff();
            BeanUtil.copyProperties(staff1,voStaff);
            System.out.println(voStaff.toString());
            voStaffs.add(voStaff);
        }
        return voStaffs;
    }

    /**
     * 获得部门
     * @param staffs
     */
    private void getDept(List<Staff> staffs){
        HashMap<Integer,Dept> map = new HashMap<>();
        for (Dept dept : deptService.list()) {
            map.put(dept.getId(),dept);
        }
        for (Staff staff : staffs) {
            staff.setDept(map.get(staff.getDid()));
        }
    }

    /**
     * 获得职位
     * @param staffs
     */
    private void getPosition(List<Staff> staffs){
        HashMap<Integer, Position> map = new HashMap<>();
        for (Position pos : positionService.list()) {
            map.put(pos.getId(),pos);
        }
        for (Staff staff : staffs) {
            staff.setPosition(map.get(staff.getPid()));
        }
    }
}
